package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ty.basic.constant.ApiPathConstant;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.session.TyAccountContext;
import com.ty.basic.utils.ArgumentUtils;
import cqrtplm.service.FlowControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(ApiPathConstant.PRODUCT_VERSION_ONE_PATH+"rt/workflow")
@Api(tags = "流程相关")
@Slf4j
public class RTWorkFlowController {

    @Resource
    private FlowControlService flowControlService;
    @GetMapping("processTaskClaim")
    @ApiOperation(value = "流程任务认领", produces = "application/json; charset=utf-8")
    public ResponseResult processTaskClaim(@Param("taskDefKey") String taskDefKey,@Param("workFlowContainerId") String workFlowContainerId, @Param("taskId") String taskId) {
        log.info("流程任务认领");
        UserDO user = TyAccountContext.getUser();
        ArgumentUtils.checkArgumentNull(taskDefKey, "taskDefKey");
        ArgumentUtils.checkArgumentNull(workFlowContainerId, "workFlowContainerId");
        ArgumentUtils.checkArgumentNull(workFlowContainerId, "taskId");

        log.info("任务ID：{}", taskId);
        return ResponseResult.success(flowControlService.processTaskClaim(taskDefKey, workFlowContainerId , taskId, user));
    }

    @GetMapping("selectClaimMsg")
    @ApiOperation(value = "查询流程任务认领信息", produces = "application/json; charset=utf-8")
    public ResponseResult selectClaimMsg(@Param("taskDefKey") String taskDefKey,
                                         @Param("workFlowContainerId") String workFlowContainerId) {
        ArgumentUtils.checkArgumentNull(taskDefKey, "taskDefKey");
        ArgumentUtils.checkArgumentNull(workFlowContainerId, "workFlowContainerId");
        String taskId = taskDefKey + ":" + workFlowContainerId;
        return ResponseResult.success(flowControlService.selectClaimMsgByTaskId(taskId));
    }

    @PostMapping("startIpdWorkFlow")
    @ApiOperation(value = "自动发起解密流程", produces = "application/json; charset=utf-8")
    public ResponseResult startIpdWorkFlow(@RequestBody JSONObject params) {
        flowControlService.startIpdWorkFlow(params);
        return ResponseResult.success();
    }

    @GetMapping("checkIsNeedClaim")
    @ApiOperation(value = "查询流程任务认领信息", produces = "application/json; charset=utf-8")
    public ResponseResult checkIsNeedClaim(String taskId) {
        Boolean b = flowControlService.checkIsNeedClaim(taskId);
        // true 置灰 不可点击  false 不置灰 可点击
        return ResponseResult.success(b);
    }

    @PostMapping("startPlanChangWorkFlow")
    @ApiOperation(value = "自动发起计划变更流程", produces = "application/json; charset=utf-8")
    public ResponseResult startPlanChangWorkFlow(@RequestBody JSONObject param) {
        IdentifierEntity data = flowControlService.startPlanChangWorkFlow(param);
        return ResponseResult.success(data);
    }

    @PostMapping("completedPlanChangWorkFlow")
    @ApiOperation(value = "自动完成计划变更流程", produces = "application/json; charset=utf-8")
    public ResponseResult completedPlanChangWorkFlow(@RequestBody JSONObject param) {
        try {
            flowControlService.completedPlanChangWorkFlow(param);
        } catch (Exception e) {
            return ResponseResult.fail("400", e.getMessage());
        }
        return ResponseResult.success();
    }
}
